package school;

import java.util.Calendar;
import java.util.Date;

public class Person {

	String firstName;
	String lastName;

	Date dateOfBirth;

	public Person() {

	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public String getAge() {
		Date DOB = getDateOfBirth();
		if (DOB == null) {
			return "Unknown";
		}

		Calendar now = Calendar.getInstance();
		now.setTimeInMillis(System.currentTimeMillis());
		if (DOB.getTime() - now.getTimeInMillis() >= 0) {
			return "Unknown";
		}

		Calendar age = Calendar.getInstance();
		age.setTimeInMillis(DOB.getTime());
		int year = now.get(Calendar.YEAR) - age.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) - age.get(Calendar.MONTH);
		int day = now.get(Calendar.DATE) - age.get(Calendar.DATE);

		if (day < 0) {
			month -= 1;
		}

		if (month < 0) {
			year -= 1;
			month += 12;
		}

		if (year < 3) {
			return year * 12 + month + " months";
		} else {
			String str = year + " years";
			if (month > 0) {
				str += " and " + month + " months";
			}
			return str;
		}
	}
}
